/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.base.entity.Customer;
import com.chlitina.store.modules.expert.dao.CustomerExamDao;
import com.chlitina.store.modules.expert.entity.CustomerExam;
import com.chlitina.store.modules.expert.entity.CustomerSolution;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 产品Service
 * @author lvbo
 * @version 2013-06-04
 */
@Component
@Transactional(readOnly = true)
public class CustomerExamService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CustomerExamService.class);
	
	@Autowired
	private CustomerExamDao customerExamDao;

	public CustomerExam get(Long id) {
		return customerExamDao.findOne(id);
	}
	
	public Page<CustomerExam> find(Page<CustomerExam> page, CustomerExam customerExam, 
			Long customerId, String type, String keyWord, boolean justShowToday) {
		page.setPageSize(Integer.parseInt(Global.getConfig("page.longPageSize")));
		DetachedCriteria dc = customerExamDao.createDetachedCriteria();
		dc.add(Restrictions.eq("store", StoreUtils.getStore()));
		if (customerId != null){
			dc.add(Restrictions.eq("customer.id", customerId));
		}
		dc.createAlias("customer", "c");
		
		if (keyWord == null) {
			keyWord = "";
		}
		if (type == null || "0".equals(type)) {
			dc.add(Restrictions.like("c.name", "%"+keyWord+"%"));
		} else if ("1".equals(type)) {
			dc.add(Restrictions.like("c.nickname", "%"+keyWord+"%"));
		} else if ("2".equals(type)) {
			dc.add(Restrictions.like("c.mobile", "%"+keyWord+"%"));
		}
		if(justShowToday) {
			Date today = new Date();
			dc.add(Restrictions.between("exam_time", DateUtils.getBeginOfDay(today), DateUtils.getEndOfDay(today)));	
		}
		dc.addOrder(Order.desc("id"));
		return customerExamDao.find(page, dc);
	}
	public Page<CustomerExam> findOngoingSolution(Page<CustomerExam> page, CustomerExam customerExam, String searchName) {
		String sql = "select c from CustomerSolution cs, CustomerExam c where cs.status=0 AND c.store.id = " + StoreUtils.getStore().getId() + " AND c.customer.name like ?1 AND cs.customerExam.id = c.id";
		if (StringUtils.isEmpty(searchName)) {
			return null;
		}
		return customerExamDao.find(page, sql, "%" + searchName + "%");
	}
	
	@Transactional(readOnly = false)
	public void save(CustomerExam customerExam) {
		customerExamDao.save(customerExam);
	}
	
	@Transactional(readOnly = false)
	public void delete(CustomerExam customerExam) {
		customerExamDao.delete(customerExam);
	}
	
	@Transactional(readOnly = false)
	public List<Long> getMaxIDsByStore(Long store_id) {
		return (List<Long>) customerExamDao.getMaxIDsByStore(store_id);
	}
	
	@Transactional(readOnly = false)
	public List<Customer> findCustomerByEmployeeId(int status, Long employeeId) {
		return customerExamDao.getCustomerByEmployee(status, employeeId);
	}
	
	@Transactional(readOnly = false)
	public List<CustomerExam> findAll() {
		return (List<CustomerExam>) customerExamDao.findAll();
	}
	
	//判断当前用户是否有开单没有方案或有未完成的方案
	public boolean hasExpert(Long customerId) {
		DetachedCriteria dc = customerExamDao.createDetachedCriteria();
		dc.add(Restrictions.eq("customer.id", customerId));
		dc.addOrder(Order.desc("id"));
		List<CustomerExam> list = customerExamDao.find(dc);
		boolean ifHave = false;
		for (CustomerExam customerExam : list) {
			List<CustomerSolution> csList = customerExam.getCustomerSolutionList();
			if(csList.size() == 0 ) {
				ifHave = true;
			} else {
				for (CustomerSolution customerSolution : csList) {
					if(customerSolution.getStatus() == CustomerSolution.PROCEED_STATUS) {
						ifHave = true;
						break;
					}
				}
			}
			if(ifHave) {
				break;
			}
		}
		return ifHave;
	}
}
